{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More or less Yolo, no help or indication of something that might go wrong, no typing, no checks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def calculate_average(numbers):\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "\n",
    "calculate_average([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform runtime checks before calling the function that raise errors if the types do not match up, interrupting execution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "# check that its of type list and contains integers\n",
    "assert isinstance(my_list, list)\n",
    "assert all(isinstance(x, int) for x in my_list)\n",
    "\n",
    "calculate_average(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Runtime checks inside the function which can raise errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def calculate_average_safe(numbers):\n",
    "    if not isinstance(numbers, list):\n",
    "        raise ValueError(\"Input must be a list.\")\n",
    "\n",
    "    if not all(isinstance(num, (int, float)) for num in numbers):\n",
    "        raise ValueError(\"All elements in the list must be integers or floats.\")\n",
    "\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "\n",
    "calculate_average_safe([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No explicit checks but give the developer an indication of what kind of thing the function expects (by using type annotations):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_average_typed(numbers: list[int | float]) -> float:\n",
    "    return sum(numbers) / len(numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Watch out as it's easy to make type constraints too strict. `calculate_average_typed` now only expects a list, but it should also work with other data structures such as a set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from typing import Iterable\n",
    "\n",
    "\n",
    "def calculate_average_typed_v2(numbers: Iterable[int | float]) -> float:\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "\n",
    "calculate_average_typed_v2((1, 2, 3, 4, 5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
